package clang.ir;

import clang.Datatype;
import fixedpoint.DataflowException;

public class Multiply extends Operator {

    public Multiply() {
        child = new Node[2];
    }

    @Override
    public String getRuntimeExpression() throws DataflowException {
        String[] runtimeValue = new String[child.length];
        for (int i = 0; i < child.length; i++) {
            runtimeValue[i] = child[i].getRuntimeValueWithWrap();

            // force cast
            // must be improved (but works for now)
            Datatype nodeDatatype = child[i].datatype;
            if (datatype != null) {
                boolean flag = false;
                // if the product is long and the operand is short then cast the operand to long
                if ((datatype == Datatype.SIGNED_LONG) || (datatype == Datatype.UNSIGNED_LONG)) {
                    if (nodeDatatype != null) {
                        if (nodeDatatype == Datatype.SIGNED_SHORT || nodeDatatype == Datatype.UNSIGNED_SHORT) {
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    // cast explicitly
                    runtimeValue[i] = wrap(datatype.toString()) + runtimeValue[i];
                }
            }
        }

        return runtimeValue[0] + " * " + runtimeValue[1];
    }
}
